package com.imchen.cms.cmscommon.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * @author imchen
 * @create: 2019-01-17 23:21
 **/
public class JsonUtil {

    private static ObjectMapper objectMapper =null;

    public static String objectToJson(Object object) {
        if (objectMapper==null){
            objectMapper=new ObjectMapper();
        }
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T JsonToObject(String json,Class<T> valueType) {
        if (objectMapper==null){
            objectMapper=new ObjectMapper();
        }
        try {
            return objectMapper.readValue(json,valueType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
